package cn.zhangxh.base.service.impl;

import cn.zhangxh.base.mapper.BaseMapper;
import cn.zhangxh.base.query.BaseQuery;
import cn.zhangxh.base.service.IBaseService;
import cn.zhangxh.org.domain.Employee;
import cn.zhangxh.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @param :
 * @Author zxh
 * @Date: 2022/1/24$ 15:09$
 * @Version 1.0
 * @return :
 * @Description: BaseServiceImpl$
 */
/*事务配置,默认是readOnly = false*/
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public T queryById(long id) {
        return baseMapper.queryById(id);
    }

    @Override
    public List<T> queryAll() {
        return baseMapper.queryAll();
    }

    @Override
    public PageInfo<T> queryPage(BaseQuery query) {
        /* Step 1: 查询总条数 */
        long count = baseMapper.count(query);
        /* Step 2: 拿到数据 */
        List<T> list = baseMapper.queryDate(query);
        /* Step 3: new 我们新建用来接收他的类 */
        return new PageInfo<T>(count,list);
    }
}
